package com.xtourms.client.rm.hotel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.ContentFrame;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.shared.field.HotelField;

public class HotelManager extends Composite {

	private static HotelSearchResultUiBinder uiBinder = GWT
			.create(HotelSearchResultUiBinder.class);

	interface HotelSearchResultUiBinder extends
			UiBinder<Widget, HotelManager> {
	}

	@UiField FlexTable resultTable;
	
	private JSONArray result;
	private ContentFrame parent;
	
	public HotelManager(JSONArray hotels, ContentFrame parent) {
		initWidget(uiBinder.createAndBindUi(this));
		this.result = hotels;
		this.parent = parent;
		initTableHeader();
		fill(hotels);
	}

	private void initTableHeader(){
		int col = 0;
		resultTable.getCellFormatter().setWidth(0, col, "30px");
		resultTable.setText(0, col++, "选项");
		resultTable.getCellFormatter().setWidth(0, col, "100px");
		resultTable.setText(0, col++, "酒店名");
		resultTable.getCellFormatter().setWidth(0, col, "50px");
		resultTable.setText(0, col++, "城市");
		resultTable.getCellFormatter().setWidth(0, col, "40px");
		resultTable.setText(0, col++, "等级");
		resultTable.setText(0, col++, "地址");
//		resultTable.setText(0, col++, "负责人");
//		resultTable.setText(0, col++, "电话");
//		resultTable.setText(0, col++, "联系人");
//		resultTable.setText(0, col++, "电话");
//		resultTable.setText(0, col++, "QQ");
		resultTable.getRowFormatter().addStyleName(0, "result_header");
	}
	
	private void fill(JSONArray hotels){
		if(null != hotels && hotels.size() > 0){
			for(int index = 0; index < hotels.size(); index++){
				fillRow(index+1, (JSONObject)hotels.get(index));
			}
		}
	}

	class detailShow implements ClickHandler {
		
		private int index;
		
		public detailShow(int index){
			this.index = index;
		}
		
		@Override
		public void onClick(ClickEvent event) {
			JSONObject hotel = (JSONObject) result.get(index);
			HotelDetail detail = new HotelDetail(hotel);
			detail.center();
		}
	}
	
	protected JSONObject getSelectedHotel(){
		if(null != result && result.size() > 0){
			for(int index = 0; index < result.size(); index++){
				RadioButton rb = (RadioButton)resultTable.getWidget(index+1, 0);
				if(Boolean.TRUE == rb.getValue()){
					return (JSONObject)result.get(index);
				}
			}
		}
		return null;
	}
	
	private void fillRow(int row, JSONObject hotel) {
		int col = 0;
		RadioButton rb = new RadioButton("hotel");
		resultTable.setWidget(row, col++, rb);
		String name = ((JSONString)hotel.get(HotelField.HOTEL_NAME)).stringValue();
		Anchor hName = new Anchor(name);
		hName.addClickHandler(new detailShow(row-1));
		resultTable.setWidget(row, col++, hName);
		resultTable.setText(row, col++, ((JSONString)hotel.get(HotelField.HOTEL_CITY_NAME)).stringValue());
		resultTable.setText(row, col++, ((JSONString)hotel.get(HotelField.HOTEL_LV)).stringValue());
		resultTable.setText(row, col++, ((JSONString)hotel.get(HotelField.HOTEL_ADD)).stringValue());
//		resultTable.setText(row, col++, ((JSONString)hotel.get(HotelField.HOTEL_MANAGER_NAME)).stringValue());
//		resultTable.setText(row, col++, ((JSONString)hotel.get(HotelField.HOTEL_MANAGER_MOBILE)).stringValue());
//		resultTable.setText(row, col++, ((JSONString)hotel.get(HotelField.HOTEL_LINKMAN_NAME)).stringValue());
//		resultTable.setText(row, col++, ((JSONString)hotel.get(HotelField.HOTEL_LINKMAN_MOBILE)).stringValue());
//		resultTable.setText(row, col++, ((JSONString)hotel.get(HotelField.HOTEL_LINKMAN_QQ)).stringValue());
		if(0 == row%2){
			resultTable.getRowFormatter().addStyleName(row, "odd");
		}else{
			resultTable.getRowFormatter().addStyleName(row, "even");
		}
	}
	
	@UiHandler("add_hotel")
	void addHotel(ClickEvent evt){
		parent.appendContent(new HotelDetailManager(parent,new AsyncCallback<String>() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(String ret) {
				JSONObject hotelObj = (JSONObject) JSONParser.parse(ret);
				resultTable.clear();
				result = new JSONArray();
				result.set(0, hotelObj);
				initTableHeader();
				fillRow(1, hotelObj);
				parent.resumeContent();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.ins.showError("酒店管理服务错误:"+caught.getMessage());
			}
		}));
	}
	
	@UiHandler("modify_hotel")
	void modifyHotel(ClickEvent evt){
		JSONObject hotelObj = null;
		int index = 0;
		if(null != result && result.size() > 0){
			for(; index < result.size(); index++){
				RadioButton rb = (RadioButton)resultTable.getWidget(index+1, 0);
				if(Boolean.TRUE == rb.getValue()){
					hotelObj = (JSONObject)result.get(index);
					break;
				}
			}
		}
		if(null != hotelObj){
			final int modifyIndex = index;
			parent.appendContent(new HotelDetailManager(hotelObj, parent, new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					MessageBox.ins.showError("酒店管理服务错误:"+caught.getMessage());
				}

				@SuppressWarnings("deprecation")
				@Override
				public void onSuccess(String ret) {
					JSONObject hotelObj = (JSONObject) JSONParser.parse(ret);
					result.set(modifyIndex, hotelObj);
					fillRow(modifyIndex+1, hotelObj);
					parent.resumeContent();
				}
			}));
		}
	}
	
	@UiHandler("room_manager")
	void manageRoom(ClickEvent evt){
		final JSONObject hotelObj = getSelectedHotel();
		if(null != hotelObj){
			JSONObject queryObj = new JSONObject();
			queryObj.put(HotelField.HOTEL_ID, hotelObj.get(HotelField.HOTEL_ID));
			HotelManagerFrame.hmsa.queryRoom(queryObj.toString(), new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					MessageBox.ins.showError("查询酒店房间服务发生错误"+caught.getMessage());
				}

				@SuppressWarnings("deprecation")
				@Override
				public void onSuccess(String result) {
					JSONArray roomsObj = (JSONArray) JSONParser.parse(result);
					parent.appendContent(new RoomManager(hotelObj, roomsObj, parent));
				}
			
			});
		} else {
			MessageBox.ins.showWarning("请选中一个酒店进行管理");
		}
	}
}
